package br.urca.impl;

import java.util.List;

import javax.inject.Inject;
import javax.persistence.EntityManager;

import br.urca.dao.AlunoIESDAO;
import br.urca.model.AlunoIES;

public class JPAAlunoIESDAO implements AlunoIESDAO {

	private final EntityManager manager;
	
	public JPAAlunoIESDAO() {
		this(null);
	}
	
	@Inject
	public JPAAlunoIESDAO(EntityManager manager) {
		this.manager = manager;
	}
	
	@Override
	public AlunoIES getRegistro(String periodo, String numProtocolo,
			String matricula) {
		try {
			return this.manager
					.createQuery(
							"SELECT a FROM AlunoIES a "
									+ "WHERE a.id.periodo = :periodo "
									+ "AND a.id.numProtocolo = :protocolo "
									+ "AND a.id.matricula = :matricula ",
							AlunoIES.class).setParameter("periodo", periodo)
					.setParameter("protocolo", numProtocolo)
					.setParameter("matricula", matricula).getSingleResult();
		} catch (Exception e) {
			return null;
		}
	}

	@Override
	public boolean salvarRegistro(AlunoIES alunoIES) {
		return this.manager.merge(alunoIES) != null;
	}

	@Override
	public List<AlunoIES> getRegistros(String periodo, String matricula) {
		try {
			return this.manager
					.createQuery(
							"SELECT a FROM AlunoIES a "
									+ "WHERE a.id.periodo = :periodo "
									+ "AND a.id.matricula = :matricula ",
							AlunoIES.class).setParameter("periodo", periodo)
					.setParameter("matricula", matricula).getResultList();
		} catch (Exception e) {
			return null;
		}
	}

	
}
